/* This is a template command. */
CmdUtils.CreateCommand({
  name: "quick-scriptures",
  icon: "http://scriptures.lds.org/scriptures/images/scriptures.png",
  author: {name: "David Ockey"},
  license: "GPL",
  description: "Quickly navigate the Scriptures on LDS.org",
  help: "type: book chapter verse",
  takes: {"input": /.*/},
  preview: function(pblock, input) {
    var booknum = input.text.match(/^\d/);
    var bookCapt = input.text.match(/[a-zA-Z][a-zA-Z \&\+]+/);
    var book = new String(bookCapt);
 
    var bsend;
    if (book.match(/^nep/i)) {
      bsend = booknum + " Nephi";
    }
    else if (book.match(/^d.?c|doc/i)) {
      bsend = "D&C";
    }
    else if (book.match(/^wor/i)) {
      bsend = "Words of Mormon";
    }
    else if (book.match(/^jac/i)) {
      bsend = "Jacob";
    }
    else if (book.match(/^eno/i)) {
      bsend = "Enos";
    }
    else if (book.match(/^jar/i)) {
      bsend = "Jarom";
    }
    else if (book.match(/^omn/i)) {
      bsend = "Omni";
    }
    else if (book.match(/^mosi/i)) {
      bsend = "Mosiah";
    }
    else if (book.match(/^alm/i)) {
      bsend = "Alma";
    }
    else if (book.match(/^hel/i)) {
      bsend = "Helaman";
    }
    else if (book.match(/^morm/i)) {
      bsend = "Mormon";
    }
    else if (book.match(/^eth/i)) {
      bsend = "Ether";
    }
    else if (book.match(/^moro/i)) {
      bsend = "Moroni";
    }
    else if (book.match(/^mose/i)) {
      bsend = "Moses";
    }
    else if (book.match(/^abr/i)) {
      bsend = "Abraham";
    }
    else if (book.match(/^gen/i)) {
      bsend = "Genesis";
    }
    else if (book.match(/^ex/i)) {
      bsend = "Exodus";
    }
    else if (book.match(/^lev/i)) {
      bsend = "Leviticus";
    }
    else if (book.match(/^num/i)) {
      bsend = "Numbers";
    }
    else if (book.match(/^deut/i)) {
      bsend = "Deuteronomy";
    }
    else if (book.match(/^josh/i)) {
      bsend = "Joshua";
    }
    else if (book.match(/^judg/i)) {
      bsend = "Judges";
    }
    else if (book.match(/^rut/i)) {
      bsend = "Ruth";
    }
    else if (book.match(/^sam/i)) {
      bsend = booknum + " Samuel";
    }
    else if (book.match(/^kin|kng|kgs/i)) {
      bsend = booknum + " Kings";
    }
    else if (book.match(/^chr/i)) {
      bsend = booknum + " Chronicles";
    }
    else if (book.match(/^ez/i)) {
      bsend = "Ezra";
    }
    else if (book.match(/^neh/i)) {
      bsend = "Nehemiah";
    }
    else if (book.match(/^es/i)) {
      bsend = "Esther";
    }
    else if (book.match(/^job/i)) {
      bsend = "Job";
    }
    else if (book.match(/^ps/i)) {
      bsend = "Psalms";
    }
    else if (book.match(/^pr/i)) {
      bsend = "Proverbs";
    }
    else if (book.match(/^ec/i)) {
      bsend = "Ecclesiates";
    }
    else if (book.match(/^song/i)) {
      bsend = "Songs of Solomon";
    }
    else if (book.match(/^is/i)) {
      bsend = "Isaiah";
    }
    else if (book.match(/^jer/i)) {
      bsend = "Jeramiah";
    }
    else if (book.match(/^la/i)) {
      bsend = "Lamentations";
    }
    else if (book.match(/^ez/i)) {
      bsend = "Ezekiel";
    }
    else if (book.match(/^da/i)) {
      bsend = "Daniel";
    }
    else if (book.match(/^ho/i)) {
      bsend = "Hosea";
    }
    else if (book.match(/^joe/i)) {
      bsend = "Joel";
    }
    else if (book.match(/^amo/i)) {
      bsend = "Amos";
    }
    else if (book.match(/^ob/i)) {
      bsend = "Obadiah";
    }
    else if (book.match(/^jon/i)) {
      bsend = "Jonah";
    }
    else if (book.match(/^mi/i)) {
      bsend = "Micah";
    }
    else if (book.match(/^na/i)) {
      bsend = "Nahum";
    }
    else if (book.match(/^hab/i)) {
      bsend + "Habukuk";
    }
    else if (book.match(/^zep/i)) {
      bsend = "Zephaniah";
    }
    else if (book.match(/^hag/i)) {
      bsend = "Hagai";
    }
    else if (book.match(/^zec|zek/i)) {
      bsend = "Zechariah";
    }
    else if (book.match(/^mal/i)) {
      bsend = "Malachi";
    }
        else if (book.match(/^mat/i)) {
      bsend = "St. Matthew";
    }
    else if (book.match(/^mar/i)) {
      bsend = "St. Mark";
    }
    else if (book.match(/^lu/i)) {
      bsend = "St. Luke";
    }
    else if (book.match(/^joh|st\.\s?joh/i)) {
      if (booknum && booknum <= 3) {
        bsend = booknum + " John";
      }
      else if (!(booknum)) {
        bsend = "St. John";
      }
    }
    else if (book.match(/^act|the\sact/i)) {
      bsend = "The Acts";
    }
    else if (book.match(/^rom/i)) {
      bsend = "Romans";
    }
    else if (book.match(/^cor/i) && booknum) {
      bsend = booknum + " Corinthians";
    }
    else if (book.match(/^gal/i)) {
      bsend = "Galatians";
    }
    else if (book.match(/^eph/i)) {
      bsend = "Ephesians";
    }
    else if (book.match(/^phili|philli/i)) {
      bsend = "Philippians";
    }
    else if (book.match(/^col/i)) {
      bsend = "Colossians";
    }
    else if (book.match(/^the/i) && booknum) {
      bsend = booknum + " Thesselonians";
    }
    else if (book.match(/^tim/i) && booknum) {
      bsend = booknum + " Timothy";
    }
    else if (book.match(/^tit/i)) {
      bsend = "Titus";
    }
    else if (book.match(/^phile|phille/i)) {
      bsend = "Philemon";
    }
    else if (book.match(/^heb/i)) {
      bsend = "Hebrews";
    }
    else if (book.match(/^jam/i)) {
      bsend = "James";
    }
    else if (book.match(/^pet/i) && booknum) {
      bsend = booknum + " Peter";
    }
    else if (book.match(/^jud/i)) {
      bsend = "Jude";
    }
    else if (book.match(/^rev/i)) {
      bsend = "Revelation";
    }
 
    var chap;
    if (bsend) {
      if (input.text.match(/^\d?\D+(\d+)/) && input.text.match(/^\d?\D+(\d+)/).length >= 2) {
        chap = input.text.match(/^\d?\D+(\d+)/)[1];
      }
    }
    var verse;
    if (chap) {
      if (input.text.match(/^\d?\D+\d+\D+(\d+)/) && input.text.match(/^\d?\D+\d+\D+(\d+)/).length >= 2) {
        verse = input.text.match(/^\d?\D+\d+\D(\d+[-]?\d*)/)[1];
        if (verse.match(/^\d+[-]\d+$/)) {
        	verse = verse.match(/^\d+[-]\d+$/);
        }
      }
    }
    
    var block = "Best match: <b>";
    if (bsend) {
      block += bsend;
      if (chap) {
        block += " " + chap;
    if (verse) {
          block += ":" + verse;
    }
      }
    }
    block += "</b>";
    pblock.innerHTML = block;
  },
  execute: function(input) {
    var booknum = input.text.match(/^\d/);
    var book = new String(input.text.match(/[a-zA-Z][a-zA-Z\&\+]+\S/));
    var bsend;
    var bom = false;
    if (book.match(/^nep/i)) {
      bsend = booknum + "_ne";
      bom = true;
    }
    else if (book.match(/^d.?c|doc/i)) {
      bsend = "dc";
      bom = true;
    }
    else if (book.match(/^wor/i)) {
      bsend = "w_of_m";
      bom = true;
    }
    else if (book.match(/^jac/i)) {
      bsend = "jacob";
      bom = true;
    }
    else if (book.match(/^eno/i)) {
      bsend = "enos";
      bom = true;
    }
    else if (book.match(/^jar/i)) {
      bsend = "jarom";
      bom = true;
    }
    else if (book.match(/^omn/i)) {
      bsend = "omni";
      bom = true;
    }
    else if (book.match(/^mosi/i)) {
      bsend = "mosiah";
      bom = true;
    }
    else if (book.match(/^alm/i)) {
      bsend = "alma";
      bom = true;
    }
    else if (book.match(/^hel/i)) {
      bsend = "hel";
      bom = true;
    }
    else if (book.match(/^morm/i)) {
      bsend = "morm";
      bom = true;
    }
    else if (book.match(/^eth/i)) {
      bsend = "ether";
      bom = true;
    }
    else if (book.match(/^moro/i)) {
      bsend = "moro";
      bom = true;
    }
    else if (book.match(/^mose/i)) {
      bsend = "moses";
      bom = true;
    }
    else if (book.match(/^abr/i)) {
      bsend = "abr";
      bom = true;
    }
    else if (book.match(/^gen/i)) {
      bsend = "gen";
    }
    else if (book.match(/^ex/i)) {
      bsend = "ex";
    }
    else if (book.match(/^lev/i)) {
      bsend = "lev";
    }
    else if (book.match(/^num/i)) {
      bsend = "num";
    }
    else if (book.match(/^deut/i)) {
      bsend = "deut";
    }
    else if (book.match(/^josh/i)) {
      bsend = "josh";
    }
    else if (book.match(/^judg/i)) {
      bsend = "judg";
    }
    else if (book.match(/^rut/i)) {
      bsend = "ruth";
    }
    else if (book.match(/^sam/i)) {
      bsend = booknum + "_sam";
    }
    else if (book.match(/^kin|kng|kgs/i)) {
      bsend = booknum + "_kgs";
    }
    else if (book.match(/^chr/i)) {
      bsend = booknum + "_chr";
    }
    else if (book.match(/^ez/i)) {
      bsend = "ezra";
    }
    else if (book.match(/^neh/i)) {
      bsend = "neh";
    }
    else if (book.match(/^es/i)) {
      bsend = "esth";
    }
    else if (book.match(/^job/i)) {
      bsend = "job";
    }
    else if (book.match(/^ps/i)) {
      bsend = "ps";
    }
    else if (book.match(/^pr/i)) {
      bsend = "prov";
    }
    else if (book.match(/^ec/i)) {
      bsend = "eccl";
    }
    else if (book.match(/^song/i)) {
      bsend = "song";
    }
    else if (book.match(/^is/i)) {
      bsend = "isa";
    }
    else if (book.match(/^jer/i)) {
      bsend = "jer";
    }
    else if (book.match(/^la/i)) {
      bsend = "lam";
    }
    else if (book.match(/^ez/i)) {
      bsend = "ezek";
    }
    else if (book.match(/^da/i)) {
      bsend = "dan";
    }
    else if (book.match(/^ho/i)) {
      bsend = "hosea";
    }
    else if (book.match(/^joe/i)) {
      bsend = "joel";
    }
    else if (book.match(/^amo/i)) {
      bsend = "amos";
    }
    else if (book.match(/^ob/i)) {
      bsend = "obad";
    }
    else if (book.match(/^jon/i)) {
      bsend = "jonah";
    }
    else if (book.match(/^mi/i)) {
      bsend = "micah";
    }
    else if (book.match(/^na/i)) {
      bsend = "nahum";
    }
    else if (book.match(/^hab/i)) {
      bsend + "hab";
    }
    else if (book.match(/^zep/i)) {
      bsend = "zeph";
    }
    else if (book.match(/^hag/i)) {
      bsend = "hag";
    }
    else if (book.match(/^zec|zek/i)) {
      bsend = "zech";
    }
    else if (book.match(/^mal/i)) {
      bsend = "mal";
    }
    else if (book.match(/^mat/i)) {
      bsend = "matt";
    }
    else if (book.match(/^mar/i)) {
      bsend = "mark";
    }
    else if (book.match(/^lu/i)) {
      bsend = "luke";
    }
    else if (book.match(/^joh|st\.\s?joh/i)) {
      if (booknum && booknum <= 3) {
        bsend = booknum + "_jn";
      }
      else if (!(booknum)) {
        bsend = "john";
      }
    }
    else if (book.match(/^act|the\sact/i)) {
      bsend = "acts";
    }
    else if (book.match(/^rom/i)) {
      bsend = "rom";
    }
    else if (book.match(/^cor/i) && booknum) {
      bsend = booknum + "_cor";
    }
    else if (book.match(/^gal/i)) {
      bsend = "gal";
    }
    else if (book.match(/^eph/i)) {
      bsend = "eph";
    }
    else if (book.match(/^phili|philli/i)) {
      bsend = "philip";
    }
    else if (book.match(/^col/i)) {
      bsend = "col";
    }
    else if (book.match(/^the/i) && booknum) {
      bsend = booknum + "_thes";
    }
    else if (book.match(/^tim/i) && booknum) {
      bsend = booknum + "_tim";
    }
    else if (book.match(/^tit/i)) {
      bsend = "titus";
    }
    else if (book.match(/^phile|phille/i)) {
      bsend = "philem";
    }
    else if (book.match(/^heb/i)) {
      bsend = "heb";
    }
    else if (book.match(/^jam/i)) {
      bsend = "james";
    }
    else if (book.match(/^pet/i) && booknum) {
      bsend = booknum + "_pet";
    }
    else if (book.match(/^jud/i)) {
      bsend = "jude";
    }
    else if (book.match(/^rev/i)) {
      bsend = "rev";
    }
    else {
      displayMessage("Sorry, this is not implemented, yet!");
    }
   
    var chap;
    if (bsend) {
      if (input.text.match(/^\d?\D+(\d+)/) && input.text.match(/^\d?\D+(\d+)/).length >= 2) {
        chap = input.text.match(/^\d?\D+(\d+)/)[1];
      }
    }
    var verse;
    var isRange = false;
    if (chap) {
      if (input.text.match(/^\d?\D+\d+\D+(\d+)/) && input.text.match(/^\d?\D+\d+\D+(\d+)/).length >= 2) {
        verse = input.text.match(/^\d?\D+\d+\D(\d+[-]?\d*)/)[1];
        if (verse.match(/^\d+[-]\d+$/)) {
        	verse = "/" + verse.match(/^\d+[-]\d+$/) + "#" + verse.match(/^\d+/);
        	isRange = true;
        }
      }
    }
   
    var urlString;
    urlString = "http://scriptures.lds.org/"
    urlString += "en/";
    urlString += bsend ;
    if (chap) {
      urlString += "/" + chap;
      if (verse && !(isRange)) {
        urlString += "#" + verse;
      }
      else if (verse && isRange) {
        urlString += verse;
      }
    }
    else {
      urlString += "/contents";
    }
    var doc = Application.activeWindow.activeTab.document;
    var uri = Utils.url(doc.documentURI);

    try {
          uri.host.match(/scriptures\.lds\.org/i);
       if (uri.host.match(/scriptures\.lds\.org/i)) {
         doc.location.href = urlString;
       }
       else {
         Utils.openUrlInBrowser(urlString);
       }
    }
    catch (e) {
      doc.location.href = urlString;
    }
  }
});
